﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace KPWeb.Reports
{
    public class LocalReport
    {
        private static Microsoft.Reporting.WebForms.LocalReport PobierzRaport(string nazwaRaportu)
        {
            Microsoft.Reporting.WebForms.LocalReport report = new Microsoft.Reporting.WebForms.LocalReport();
            System.IO.StreamReader raportStream = ReportStream(nazwaRaportu);
            report.LoadReportDefinition(raportStream);
            raportStream.Close();
            //report.ReportPath = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/"), "Raporty", nazwaRaportu);
            return report;
        }

        public static Microsoft.Reporting.WebForms.LocalReport GenerujRaport(string nazwaRaportu)
        {
            Microsoft.Reporting.WebForms.LocalReport report = PobierzRaport(nazwaRaportu);
            return report;
        }
        public static Microsoft.Reporting.WebForms.LocalReport GenerujRaport(string nazwaRaportu,List<Microsoft.Reporting.WebForms.ReportParameter> param)
        {
            Microsoft.Reporting.WebForms.LocalReport report = PobierzRaport(nazwaRaportu);
            report.SetParameters(param);
            return report;
        }
        public static Microsoft.Reporting.WebForms.LocalReport GenerujRaport(string nazwaRaportu, Microsoft.Reporting.WebForms.ReportDataSource rds)
        {
            Microsoft.Reporting.WebForms.LocalReport report = PobierzRaport(nazwaRaportu);
            report.DataSources.Add(rds);
            return report;
        }
        public static Microsoft.Reporting.WebForms.LocalReport GenerujRaport(string nazwaRaportu, Microsoft.Reporting.WebForms.ReportDataSource rds, List<Microsoft.Reporting.WebForms.ReportParameter> param)
        {
            Microsoft.Reporting.WebForms.LocalReport report = PobierzRaport(nazwaRaportu);
            report.SetParameters(param);
            report.DataSources.Add(rds);
            return report;
        }
        public static Microsoft.Reporting.WebForms.LocalReport GenerujRaport(string nazwaRaportu, IEnumerable<Microsoft.Reporting.WebForms.ReportDataSource> rds, List<Microsoft.Reporting.WebForms.ReportParameter> param)
        {
            Microsoft.Reporting.WebForms.LocalReport report = PobierzRaport(nazwaRaportu);
            if (rds.Any())
                    foreach (Microsoft.Reporting.WebForms.ReportDataSource r in rds)
                        report.DataSources.Add(r);
                report.SetParameters(param);
       
            return report;
        }

        public static System.IO.StreamReader ReportStream(string nazwaRaportu)
        {
            return new System.IO.StreamReader(System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/"), "Reports", nazwaRaportu));
        }
        public static void Export(Microsoft.Reporting.WebForms.LocalReport report, string format, string filename = "MyReport.xls")
        {

            //Warning[] warnings;
            string[] streamids;
            string mimeType;
            string encoding;
            string extension;
            string deviceInfo;
            string contentType;

            switch (format)
            {
                case "PDF":
                    deviceInfo =
                    "<DeviceInfo>" +
                    " <OutputFormat>PDF</OutputFormat>" +
                    " <PageWidth>8.5in</PageWidth>" +
                    " <PageHeight>11in</PageHeight>" +
                    " <MarginTop>1in</MarginTop>" +
                    " <MarginLeft>1in</MarginLeft>" +
                    " <MarginRight>1in</MarginRight>" +
                    " <MarginBottom>1in</MarginBottom>" +
                    "</DeviceInfo>";
                    contentType = "application/pdf";
                    break;
                case "Excel":
                    deviceInfo =
                    "<DeviceInfo>" +
                    " <SimplePageHeaders>False</SimplePageHeaders>" +
                    "</DeviceInfo>";
                    contentType = "application/vnd.ms-excel";
                    break;

                default:
                    deviceInfo =
                    "<DeviceInfo>" +
                    " <OutputFormat>PDF</OutputFormat>" +
                    " <PageWidth>8.5in</PageWidth>" +
                    " <PageHeight>11in</PageHeight>" +
                    " <MarginTop>1in</MarginTop>" +
                    " <MarginLeft>1in</MarginLeft>" +
                    " <MarginRight>1in</MarginRight>" +
                    " <MarginBottom>1in</MarginBottom>" +
                    "</DeviceInfo>";
                    contentType = "application/pdf";
                    break;
            }

            byte[] bytes = report.Render(format);//, deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
       //     HttpContext.Current.Response.ContentType = contentType;

            //if (format == "Excel") 
            HttpContext.Current.Response.AddHeader("Content-disposition", "attachment; filename=" + filename);

            HttpContext.Current.Response.BinaryWrite(bytes);
            HttpContext.Current.Response.End();
        }



    }
}